﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Crossroad.Utils.Extensions
{
    /// <summary>
    /// 日期时间类型扩展
    /// 编码：陈冉
    /// 2018-2-27 13:51:32
    /// </summary>
    public static class DateTimeExt
    {
        #region 月份操作
        /// <summary>
        /// 取得某月的第一天
        /// </summary>
        /// <param name="datetime">要取得月份第一天的时间</param>
        /// <returns></returns>
        public static DateTime FirstDayOfMonth(this DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day);
        }

        /// <summary>
        /// 取得某月的最后一天
        /// </summary>
        /// <param name="datetime">要取得月份最后一天的时间</param>
        /// <returns></returns>
        public static DateTime LastDayOfMonth(this DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);
        }

        /// <summary>
        /// 取得上个月第一天
        /// </summary>
        /// <param name="datetime">要取得上个月第一天的当前时间</param>
        /// <returns></returns>
        public static DateTime FirstDayOfPreviousMonth(this DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day).AddMonths(-1);
        }

        /// <summary>
        /// 取得上个月的最后一天
        /// </summary>
        /// <param name="datetime">要取得上个月最后一天的当前时间</param>
        /// <returns></returns>
        public static DateTime LastDayOfPrdviousMonth(this DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day).AddDays(-1);
        }

        /// <summary>
        /// 返回星期几
        /// </summary>
        /// <param name="datetime">日期</param>
        /// <returns></returns>
        public static string DayOfWeek(this DateTime datetime)
        {
            return System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(datetime.DayOfWeek);
        }

        #endregion

        #region 时间戳

        /// <summary>
        /// 转日期字符串
        /// 编码：陈冉
        /// 2018-2-27 14:18:37
        /// </summary>
        /// <param name="input"></param>
        /// <param name="format">格式 def (yyyy-MM-dd)</param>
        /// <returns></returns>
        public static string ToDateStr(this DateTime input, string format = "yyyy-MM-dd")
        {
            return input.ToString(format);
        }

        /// <summary>
        /// 转日期字数字
        /// </summary>
        /// <param name="input"></param>
        /// <param name="format">格式 def (yyyyMMdd)</param>
        /// <returns></returns>
        public static int ToDateNum(this DateTime input, string format = "yyyyMMdd")
        {
            return Convert.ToInt32(input.ToString(format));
        }

        /// <summary>
        /// 转时间字符串
        /// 编码：陈冉
        /// 2018-2-27 14:22:28
        /// </summary>
        /// <param name="input"></param>
        /// <param name="format">格式 def (HH:mm:ss)</param>
        /// <returns></returns>
        public static string ToTimeStr(this DateTime input, string format = "HH:mm:ss")
        {
            return input.ToString(format);
        }

        /// <summary>
        /// </summary>
        /// <param name="input"></param>
        /// <param name="format">格式 def (yyyy-MM-dd HH:mm:ss)</param>
        /// <returns></returns>
        public static string ToDateTimeStr(this DateTime input, string format = "yyyy-MM-dd HH:mm:ss")
        {
            return input.ToString(format);
        }

        /// <summary>
        /// 转日期时间字符串
        /// </summary>
        /// <param name="input"></param>
        /// <param name="format">格式 def (yyyy-MM-dd HH:mm:ss)</param>
        /// <returns></returns>
        public static int ToDateTimeNum(this DateTime input, string format = "yyyy-MM-dd HH:mm:ss")
        {
            return Convert.ToInt32(input.ToString(format));
        }

        /// <summary>
        /// 转时间戳
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static int ToTimeStamp(this DateTime input)
        {
            DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            return (int)(input.AddHours(-8) - Jan1st1970).TotalSeconds;
        }

        #endregion


    }
}
